package com.example.demo.multithread;

import java.util.concurrent.CountDownLatch;

public class MultithreadOrder33 {

    public static void main(String[] args) {
        CountDownLatch c1 = new CountDownLatch(0);
        CountDownLatch c2 = new CountDownLatch(1);
        CountDownLatch c3 = new CountDownLatch(1);

        Thread t1 = new Thread(new Work(c1,c2),"线程一");
        Thread t2 = new Thread(new Work(c2,c3),"线程二");
        Thread t3 = new Thread(new Work(c3,c3),"线程三");

        t1.start();
        t2.start();
        t3.start();
    }

    static class Work implements Runnable{
        CountDownLatch one;
        CountDownLatch two;

        public Work(CountDownLatch one, CountDownLatch two) {
            this.one = one;
            this.two = two;
        }

        @Override
        public void run() {
            try {
                one.await();
                System.out.println(Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                two.countDown();
            }
        }
    }
}
